<template>
  <div class='login_box'>
    <van-form @submit="onSubmit" class='form_box' v-show='flag == 0' key='1'>
      <van-field
        v-model="username"
        name="account"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名',pattern:/^\d{11}$/ }]"
      />
      <van-field
        v-model="password"
        name="password"
        type='password'
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px;">
        <van-button
          round
          block
          type="info"
          native-type="submit"
        >
          提交
        </van-button>
      </div>
    </van-form>
    <van-form @submit="onSubmit" class='form_box' v-show='flag==1' key='2'>
      <van-field
        v-model="username"
        name="account"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' ,pattern:/^\d{11}$/}]"
      />
      <van-field
        v-model="password"
        name="password"
        center
        label="短信验证码"
        placeholder="请输入短信验证码"
        :rules="[{ required: true, message: '请填写验证码' }]"
      >
        <template #button>
          <van-button size="small" type="primary" @click='getCode'>发送验证码</van-button>
        </template>
      </van-field>
      <div style="margin: 16px;">
        <van-button
          round
          block
          type="info"
          native-type="submit"
        >
          提交
        </van-button>
       
      </div>
    </van-form>
     <span @click='changeFlag'>{{flag == 0 ? "短信验证码登录" :"密码登录"}}</span>
     <router-link to="/reg">前往注册</router-link>
  </div>
</template>
<script>
// @ is an alias to /src
import {login,getCode} from '@/api/login'
export default {
  name: "Login",
   data() {
    return {
      username: '',
      password: '',
      flag:0,// 0 密码登录   1验证码登录
    };
  },
  methods: {
    onSubmit(values) {
      console.log('submit', values);
      login({...values,type:this.flag + 1}).then(data=>{
        // console.log(data)
        if(data.code == 0){
          this.$router.push('/home');
        }
      })
    },
    changeFlag(){
      this.flag = this.flag == 0 ? 1 : 0;
      // this.username = '';
      this.password = ''
    },
    getCode(){
      getCode(this.username).then(data=>{
        console.log(data)
      })
    }
  },
};
</script>
<style lang="less" scoped>
  .login_box{
    background: #eee;
    min-height: 100vh;
    .form_box{
      width: 80%;
      padding-top: 100px;
      margin: auto;
    }
  }
</style>